﻿using System;
using System.Collections.Generic;
using System.Text;
using AudioPlayer.Plugin.Storage;

namespace AudioPlayer.Storage {
	/// <summary>
	/// Contains user defined settings
	/// </summary>
	public class UserSettings : IUserSettings {
		#region Fields
		private Guid _owner;
		#endregion
		#region Constructor
		/// <summary>
		/// Initializes a new instance of the <see cref="UserSettings"/> class.
		/// </summary>
		public UserSettings() {
			
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="UserSettings"/> class.
		/// </summary>
		/// <param name="owner">The owner item.</param>
		public UserSettings(Guid owner) {
			_owner = owner;
		}
		#endregion
		#region Public Voids
		/// <summary>
		/// Adds the specified name.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="value">The value.</param>
		public void Set(string name, Object value) {
			if (this.Valid && this.HasStorage) {
				try {
					Config.Plugins.Settings.Set<Object>(_owner, name, value);
				} catch { }
			}
		}
		/// <summary>
		/// Adds the specified name.
		/// </summary>
		/// <typeparam name="TField">The type of the field.</typeparam>
		/// <param name="name">The name.</param>
		/// <param name="value">The value.</param>
		public void Set<TField>(string name, TField value) {
			if (this.Valid && this.HasStorage) {
				try {
					Config.Plugins.Settings.Set<TField>(_owner, name, value);
				} catch { }
			}
		}
		/// <summary>
		/// Removes user setting
		/// </summary>
		/// <param name="name">The name.</param>
		public void Remove(string name) {
			if (this.Valid && this.HasStorage) {
				try {
					Config.Plugins.Settings.Remove(_owner, name);
				} catch { }
			}
		}
		/// <summary>
		/// Gets the specified name.
		/// </summary>
		/// <typeparam name="TField">The type of the field.</typeparam>
		/// <param name="name">The name.</param>
		/// <returns></returns>
		public TField Get<TField>(string name) {
			if (this.Valid && this.HasStorage) {
				try {
					return Config.Plugins.Settings.Get<TField>(_owner, name);
				} catch {
					return default(TField);
				}
			}
			return default(TField);
		}
		/// <summary>
		/// Gets the specified name.
		/// </summary>
		/// <typeparam name="TField">The type of the field.</typeparam>
		/// <param name="name">The name.</param>
		/// <param name="def">The default.</param>
		/// <returns></returns>
		public TField Get<TField>(string name,TField def) {
			if (this.Valid && this.HasStorage) {
				try {
					return Config.Plugins.Settings.Get<TField>(_owner, name,def);
				} catch {
					return def;
				}
			}
			return def;
		}
		/// <summary>
		/// Gets the specified name.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <returns></returns>
		public Object Get(string name) {
			if (this.Valid && this.HasStorage) {
				try {
					return Config.Plugins.Settings.Get<Object>(_owner, name);
				} catch {
					return null;
				}
			}
			return null;
		}

		/// <summary>
		/// Gets all names with founded owner.
		/// </summary>
		/// <returns>
		/// NULL if GUID doesn't contain any values, or Array.
		/// </returns>
		public String[] Get() {
			if (this.Valid && this.HasStorage) {
				try {
					return Config.Plugins.Settings.Get(_owner);
				} catch {
					return null;
				}
			}
			return null;
		}
		#endregion
		#region Property
		/// <summary>
		/// Gets or sets the owner.
		/// </summary>
		/// <value>The owner.</value>
		public Guid Owner {
			get { return _owner; }
			set { _owner = value; }
		}
		/// <summary>
		/// Gets a value indicating whether this <see cref="UserSettings"/> is valid.
		/// </summary>
		/// <value><c>true</c> if valid; otherwise, <c>false</c>.</value>
		public bool Valid {
			get {
				if (_owner != null && _owner != Guid.Empty) {
					return true;
				}
				return false;
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance has storage.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance has storage; otherwise, <c>false</c>.
		/// </value>
		public bool HasStorage {
			get {
				if (Config.Plugins.Settings != null) {
					return true;
				}
				return false;
			}
		}
		#endregion
	}
}
